package com.wen.controller;


import com.wen.pojo.*;
import com.wen.service.ProductService;
import com.wen.utils.PageBean;
import com.wen.utils.Result;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    //分页查询全部
    @RequestMapping("/product-page-list")
    public Result<PageBean<Product>> allProductList(Integer pageNum, Integer pageSize,Product product){
        PageBean<Product> allList = productService.allList(pageNum, pageSize, product);
        return Result.ok(200,"查询成功",allList);
    }
    //查询仓库列表
    @RequestMapping("/store-list")
    public Result findStoreList(){
        List<Store> storeList = productService.findStoreList();
        return Result.ok(200,"查询成功",storeList);
    }
    //查询所有品牌
    @RequestMapping("/brand-list")
    public Result brandList(){
        List<Brand> brandList = productService.findBrandList();
        return Result.ok(200,"查询成功",brandList);
    }
    //查询所有供应商
    @RequestMapping("/supply-list")
    public Result supplyList(){
        List<Supply> supplyList = productService.allSupplyList();
        return Result.ok(200,"查询成功",supplyList);
    }
    //查询所有单位
    @RequestMapping("/unit-list")
    public Result unitList(){
        List<Unit> unitList = productService.allUnitList();
        return Result.ok(200,"查询成功",unitList);
    }
    //查询所有产地
    @RequestMapping("/place-list")
    public Result placeList(){
        List<Place> placeList = productService.allPlaceList();
        return Result.ok(200,"查询成功",placeList);
    }
    //查询所有分类树
    @RequestMapping("/category-tree")
    public Result categoryTree(){
        List<ProductType> productTypes = productService.productTypeTree();
        return Result.ok(200,"查询成功",productTypes);
    }
    //上传图片接口
    @Value("${file.upload-path}")
    private String uploadPath;
    @CrossOrigin
    @RequestMapping("/img-upload")
    public Result imgUpload(MultipartFile file) throws IOException {
        //解析类路径
        File uploadDirFile = ResourceUtils.getFile(uploadPath);
        //拿到图片上传到目录文件的磁盘路径
        String uploadDirPath = uploadDirFile.getAbsolutePath();
        //拿到上传的图片名称
        String filename = file.getOriginalFilename();
        //拿到上传的文件要保存到磁盘文件的路径
        String uploadFilePath =  uploadDirPath+"\\"+ filename;
        //上传图片
        file.transferTo(new File(uploadFilePath));

        return Result.ok(200,"上传成功",null);
    }

    //添加商品
    //存数据库地址前缀
    @Value("${file.access-path}")
    private String path;
    @RequestMapping("/product-add")
    public Result addProduct(@RequestBody Product product){
        return productService.addProduct(product,path);
    }

    //修改上下架状态
    @RequestMapping("/state-change")
    public Result updState (@RequestBody Product product){
        return productService.updState(product);
    }

    //单个删除
    @RequestMapping("/product-delete/{productId}")
    public Result delProduct (@PathVariable Integer productId){
        Integer [] ids = new Integer[] {productId};
        return productService.delAllProduct(ids);
    }
    //批量删除
    @RequestMapping("/product-list-delete")
    public Result delAllProduct(@RequestBody Integer [] ids){
        return productService.delAllProduct(ids);
    }
    //修改商品
    @RequestMapping("/product-update")
    public Result updProduct(@RequestBody Product product){
        return productService.updProduct(product,path);
    }
}
